package io.renren.modules.repair.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.repair.dto.RepairNoticeDTO;
import io.renren.modules.repair.excel.RepairNoticeExcel;
import io.renren.modules.repair.service.RepairNoticeService;
import io.renren.modules.security.user.SecurityUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 系统公告表
 *
 * @author dio 167762802@QQ.com
 * @since 1.0.0 2025-04-12
 */
@RestController
@RequestMapping("repair/repairnotice")
@Tag(name="系统公告表")
public class RepairNoticeController {
    @Autowired
    private RepairNoticeService repairNoticeService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref="String")
    })
    @RequiresPermissions("repair:repairnotice:page")
    public Result<PageData<RepairNoticeDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<RepairNoticeDTO> page = repairNoticeService.page(params);

        return new Result<PageData<RepairNoticeDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("repair:repairnotice:info")
    public Result<RepairNoticeDTO> get(@PathVariable("id") Long id){
        RepairNoticeDTO data = repairNoticeService.get(id);

        return new Result<RepairNoticeDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("repair:repairnotice:save")
    public Result save(@RequestBody RepairNoticeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        
        //自动设置创建时间、更新时间和创建人ID
        dto.setCreateTime(new Date());
        dto.setUpdateTime(new Date());
        
        //获取当前登录用户ID
        Long userId = SecurityUser.getUserId();
        dto.setCreateUserId(userId);

        repairNoticeService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("repair:repairnotice:update")
    public Result update(@RequestBody RepairNoticeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        
        //自动设置更新时间
        dto.setUpdateTime(new Date());

        repairNoticeService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("repair:repairnotice:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        repairNoticeService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("repair:repairnotice:export")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<RepairNoticeDTO> list = repairNoticeService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "系统公告表", list, RepairNoticeExcel.class);
    }

}
